package leetcode.code0397;

public class Solution397_1 {
	// java.lang.StackOverflowError
	public int integerReplacement(int n) {
		int ans = 0;
		if (n == Integer.MAX_VALUE) {
			n = ((n - 1) >> 1) + 1;
			ans += 2;
		}
		while (n > 1) {
			if ((n & 1) == 0) {
				n >>= 1;
			} else {
				if (n != 3 && ((n >> 1) & 1) == 1) {
					n += 1;
				} else {
					n -= 1;
				}
			}
			ans++;
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution397_1 so = new Solution397_1();
		int ans = so.integerReplacement(6);
		System.out.println(ans);
//		ans = so.integerReplacement(7);
//		System.out.println(ans);
		int max = Integer.MAX_VALUE;
		int a = 1073741824;
		int b = (max - 1) / 2 + 1;
		System.out.println(a == b);
	}

}
